package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.CwzbJzgc;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class CwzbJzgcForm extends BasePage implements Serializable {
    private GenericManager<CwzbJzgc, Long> cwzbJzgcManager;
    private CwzbJzgc cwzbJzgc = new CwzbJzgc();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setCwzbJzgcManager(GenericManager<CwzbJzgc, Long> cwzbJzgcManager) {
        this.cwzbJzgcManager = cwzbJzgcManager;
    }

    public CwzbJzgc getCwzbJzgc() {
        return cwzbJzgc;
    }

    public void setCwzbJzgc(CwzbJzgc cwzbJzgc) {
        this.cwzbJzgc = cwzbJzgc;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        cwzbJzgcManager.remove(cwzbJzgc.getId());
        addMessage("cwzbJzgc.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            cwzbJzgc = cwzbJzgcManager.get(id);
        } else {
            cwzbJzgc = new CwzbJzgc();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (cwzbJzgc.getId() == null || cwzbJzgc.getId() == 0);
        // set the related jbqk as a foreign key.
        cwzbJzgc.setJbqk( jbqk );
        // set username who act this operation, get the user name from httprequest
        cwzbJzgc.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        cwzbJzgc.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        cwzbJzgc.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        
        cwzbJzgcManager.save(cwzbJzgc);

        String key = (isNew) ? "cwzbJzgc.added" : "cwzbJzgc.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 // set the id to the next page
            this.id = cwzbJzgc.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 